﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Resto.CostControl.Core.Cache;
using Resto.CostControl.Core.Domain.Model;
using Resto.CostControl.Core.Enums;

namespace Resto.CostControl.Core.Facade.SiteModule.DataTransferObject
{
    /// <summary>
    /// 站点收入
    /// </summary>
    public class SiteIncomeDTO
    {
        #region 基本属性

        public string ID { get; set; }

        /// <summary>
        /// 收入的日期
        /// </summary>
        public DateTime Date { get; set; }

        /// <summary>
        /// 记录的状态,UI不要赋值
        /// </summary>
        public DataState State { get; set; }

        /// <summary>
        /// 项目点Id
        /// </summary>
        public string SiteId { get; set; }

        /// <summary>
        /// 项目点名称
        /// </summary>
        public string SiteName
        {
            get
            {
                SiteDTO siteDTO = SiteDTOCache.Get(SiteId);
                if (siteDTO != null)
                    return siteDTO.Name;
                else
                    return string.Empty;
            }
        }

        /// <summary>
        /// 当天的总收入
        /// </summary>
        public decimal? TotalAmount
        {
            get
            {
                if (SalesMethodItems != null && SalesMethodItems.Length > 0)
                {
                    decimal value = 0m;
                    foreach (SalesMethodDTO item in SalesMethodItems)
                        value += item.Amount;
                    return value;
                }

                return null;

            }
        }

        /// <summary>
        /// 消息方式项
        /// </summary>
        public SalesMethodDTO[] SalesMethodItems { get; set; }
        
        /// <summary>
        /// 销售情况项
        /// </summary>
        public SaleStatusDTO[] SaleStatusItems { get;set; }

        #endregion

        #region 构造函数

        public SiteIncomeDTO() { }

        internal SiteIncomeDTO(SiteIncome income)
        {
            this.ID = income.ID;
            this.Date = income.Date;
            this.SiteId = income.SiteID;
            this.State = (DataState)Enum.Parse(typeof(DataState), income.State);

            List<SalesMethodDTO> list1 = new List<SalesMethodDTO>();
            foreach (SalesMethod item in income.SalesMethods)
                list1.Add(new SalesMethodDTO(item));

            this.SalesMethodItems = list1.ToArray();

            List<SaleStatusDTO> list2 = new List<SaleStatusDTO>();
            foreach (SaleStatus item in income.SaleStatuses)
                list2.Add(new SaleStatusDTO(item));

            this.SaleStatusItems = list2.ToArray();
        }

        #endregion

        #region 方法

        internal SiteIncome ToSiteIncome()
        {
            SiteIncome order = new SiteIncome();
            CopyToSiteIncome(order);
            return order;
        }

        internal void CopyToSiteIncome(SiteIncome income)
        {
            income.SiteID = this.SiteId;
            income.Date = this.Date.Date;
            income.TotalAmount = this.TotalAmount;
 
            CopySalesMethodItems(income);
            CopySaleStatusItems(income);

        }

        private void CopySalesMethodItems(SiteIncome income)
        {
            //清除要删除的项
            string[] oldIds = (from o in income.SalesMethods
                            select o.ID).ToArray();

            string[] newIds = (from o in this.SalesMethodItems
                            select o.ID).ToArray();

            List<string> delIds = (from o in oldIds
                                where !newIds.Contains(o)
                                select o).ToList();

            delIds.ForEach(x => income.RemoveSalesMethod(x));


            //修改和新增项
            foreach (SalesMethodDTO itemDTO in this.SalesMethodItems)
            {
                SalesMethod item;
                if (!string.IsNullOrWhiteSpace(itemDTO.ID))
                {
                    item = income.GetSalesMethod(itemDTO.ID);
                    if (item != null)
                        itemDTO.CopyToSalesMethod(item);
                }
                else
                    income.AddSalesMethod(itemDTO.ToSalesMethod());
            }
        }

        private void CopySaleStatusItems(SiteIncome income)
        {
            //清除要删除的项
            string[] oldIds = (from o in income.SaleStatuses
                            select o.ID).ToArray();

            string[] newIds = (from o in this.SaleStatusItems
                            select o.ID).ToArray();

            List<string> delIds = (from o in oldIds
                                where !newIds.Contains(o)
                                select o).ToList();

            delIds.ForEach(x => income.RemoveSaleStatus(x));


            //修改和新增项
            foreach (SaleStatusDTO itemDTO in this.SaleStatusItems)
            {
                SaleStatus item;
                if (!string.IsNullOrWhiteSpace(itemDTO.ID))
                {
                    item = income.GetSaleStatus(itemDTO.ID);
                    if (item != null)
                        itemDTO.CopyToSaleStatus(item);
                }
                else
                    income.AddSaleStatus(itemDTO.ToSaleStatus());
            }
        }

        #endregion
    }
}
